package com.qf.smsplatform.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.DirtywordsCache;
import com.qf.smsplatform.constans.ResultCode;
import com.qf.smsplatform.events.UpdateBlackEvent;
import com.qf.smsplatform.events.UpdateDirtyWordsEvent;
import com.qf.smsplatform.exception.AddDataException;
import com.qf.smsplatform.exception.MyBaseException;
import com.qf.smsplatform.exception.QueryDataException;
import com.qf.smsplatform.exception.UpdateDataException;
import com.qf.smsplatform.mapper.TDirtywordMapper;
import com.qf.smsplatform.pojo.*;
import com.qf.smsplatform.service.TDirtywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
/*2021/10/26*/public class TDirtywordServiceImpl implements TDirtywordService {

    private TDirtywordMapper tDirtywordMapper;
    @Autowired
    public void settDirtywordMapper(TDirtywordMapper tDirtywordMapper) {
        this.tDirtywordMapper = tDirtywordMapper;
    }


    private DirtywordsCache dirtywordsCache;
    @Autowired
    public void setDirtywordsCache(DirtywordsCache dirtywordsCache) {
        this.dirtywordsCache = dirtywordsCache;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public TDirtyword findDirtyById(Long id) {
        if(ObjectUtils.isEmpty(id)||id<=0){
            throw new MyBaseException("传递的主键不合法", ResultCode.PK_NOT_ALLOW);
        }
        //获取当前登录用户对象
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        TDirtyword dirtyword = dirtywordsCache.getByKey(id);
        if(user.getType()==1||dirtyword.getCreater()==user.getId()){//验证用户是否有权限进行数据查看
            return dirtyword;
        }
        throw new QueryDataException("没有权限进行查看", ResultCode.NOT_ALLOW);

    }

    @Override
    public void addDirtyWord(TDirtyword dirtyword) {
        if (dirtyword.isNull(CheckType.ADD)) {
            throw new AddDataException("添加的敏感词汇不合法", ResultCode.DATA_ISNULL);
        }
        tDirtywordMapper.addDirtyword(dirtyword);
        //发布一个事件,缓存中的内容进行更新
        context.publishEvent(new UpdateDirtyWordsEvent());
    }

    @Override
    public PageInfo<TDirtyword> selectPage(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);//使用插件进行限定数据的查询
        List<TDirtyword> dirtywordList = dirtywordsCache.getAllData();

        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        List<TDirtyword> collect = dirtywordList.stream().filter(dirtyword -> {//对查询出的数据进行过滤，只能看到应该看的数据
            return dirtyword.getCreater() == user.getId() || dirtyword.getOwntype() == 1 || user.getType() == 1;
        }). skip((pageNum-1)*pageSize).
                limit(pageSize).
                collect(Collectors.toList());

        PageInfo<TDirtyword> pageInfo=new PageInfo<>(collect);//将查询到的数据放入pageinfo中进行传递
        return pageInfo;
    }

    @Override
    public Integer deleteByIds(List<Integer> ids) {
        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if(user.getType()==1){
            Integer integer = tDirtywordMapper.deleteByAdmin(ids);
            //发布一个事件
            context.publishEvent(new UpdateDirtyWordsEvent());
            return integer;
        }
        Integer integer = tDirtywordMapper.deleteByIds(ids, user.getId());
        //发布一个事件,缓存中的内容进行更新
        context.publishEvent(new UpdateDirtyWordsEvent());
        return integer;
    }

    @Override
    public void updateTDirtyWord(TDirtyword tDirtyword) {

        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //校验敏感词信息是否符合规则
        if (tDirtyword.isNull(CheckType.UPDATE)) {
            //不是合理的手机号
            //抛出异常
            throw new UpdateDataException("敏感词信息不符合规范", ResultCode.DATA_ISNULL);
        }
        if(user.getType()!=1 && user.getId()!= tDirtyword.getCreater()){
            throw new UpdateDataException("没有权限进行修改", ResultCode.NOT_ALLOW);
        }
        tDirtywordMapper.updateDirtyword(tDirtyword);
        //发布一个事件
        context.publishEvent(new UpdateDirtyWordsEvent());
    }
}
